package com.example.file_explorer.service;

import com.example.file_explorer.entity.FileNode;
import com.example.file_explorer.utils.IOUtils;
import com.example.file_explorer.vo.FileVo;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Service
public class SearchService implements Search{
    Set<FileVo> searchFile = new HashSet<>();
    public Set<FileVo> find(String path, String filename) {
        Set<FileNode> fileList = IOUtils.directory(path);
        searchFile.clear();
        if (fileList != null)
            dfs(fileList, filename, path);
        return searchFile;
    }

    public void dfs(Set<FileNode> set, String filename, String path) {
        for (FileNode node : set) {
            if (node.getFileName().toUpperCase().contains(filename.toUpperCase())) {
                searchFile.add(new FileVo(node, path));
            }
            if (!node.getChildren().isEmpty())
                dfs(node.getChildren(), filename, path + "/" + node.getFileName());
        }
    }
}